<?php
namespace Admin\Controller;
use Think\Model;

class ShopCategoryController extends ComController
{
    public function index()
    {
        $list = M('industry_category')->where("ind_fkey=0 AND ind_state=1")->select();
        $this->assign('list', $list);
        // $this->assign('floors', $floors);
        return $this->display();
    }

    public function add()
    {
        if (!IS_POST) {
            return $this->display();
        }

        $data = I('post.');

        if (empty($data['name'])) {
            return $this->error('参数错误！');
        }

        if (empty($data['id'])) {
            return $this->error('参数错误！');
        }

        $saveData = array(
            'ind_name' => $data['name'],
            'ind_id' => $data['id'],
            'ind_fkey' => isset($data['fkey']) && !empty($data['fkey']) ? intval($data['fkey']) :0,
        );

        $res = M('industry_category')->data($saveData)->add();

        if ($res === false) {
            return $this->error('添加失败');
        }

        return $this->success('添加成功','index');
    }

    public function del($id){
       if (empty($id)) {
        return $this->error('参数错误！');
    }

    $res = M('industry_category')->where(['ind_id'=>$id])->data(['ind_state'=>0])->save();

        if (!$res) {
            return $this->error('删除失败');
        }

        return $this->success('删除成功');
    }

    public function update()
    {
        $id = I('param.id','', 'intval');

        if (empty($id)) {
            return $this->error('参数错误!');
        }

        $m = M('floors');
        $where = "id='{$id}'";

        if(!IS_POST){
            $info = $m->where($where)->find();

            if (empty($info)) {
                return $this->error('获取失败!');
            }

            $this->assign('info', $info);
            return $this->display();
        }

        $data = I('post.');
        $saveData = array(
            'name' => $data['floorName'],
            'code' => $data['code'],
            'img' => $data['pic'],
            'sort' => $data['sort'],
        );

        $res = $m->data($saveData)->where($where)->save();

        if ($res === false) {
           return $this->error('修改失败');
        }else{
           return $this->success('修改成功');
        }
    }
}
